package jp.gr.java_conf.ryz.summarize.client.view;

import java.util.ArrayList;
import java.util.List;

import jp.gr.java_conf.ryz.summarize.client.presenter.PostDecorationPair;
import jp.gr.java_conf.ryz.summarize.shared.model.Post;

import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.SimplePanel;

public class ViewerPanel extends SimplePanel
{
	private static final String DefaultStyleName = "viewerPanel";
	
	private List<PostPanel> _postPanels = new ArrayList<PostPanel>();
	private Panel _mainPanel = new FlowPanel();
	
	
	/**
	 * default constructor.
	 */
	public ViewerPanel()
	{
		super();
		
		init();
	}
	
	
	@Override
	public void clear()
	{
//		super.clear();
		
		_mainPanel.clear();
		_postPanels.clear();
	}
	
	/**
	 * set all post.
	 * 
	 * @param posts posts
	 */
	public void setPosts(List<Post> posts)
	{
		clear();
		for (Post each : posts)
		{
			_mainPanel.add(createPostPanel(each));
		}
	}
	
	/**
	 * get selected all PostDecorationPair.
	 * 
	 * @return selected all PostDecorationPair
	 */
	public List<PostDecorationPair> getSelectedPostDecorationPairs()
	{
		List<PostDecorationPair> result = new ArrayList<PostDecorationPair>();
		
		for (PostPanel each : _postPanels)
		{
			if (each.isSelected())
			{
				result.add(new PostDecorationPair(each.getPost(), each.getPostDecoration()));
			}
		}
		
		return result;
	}
	
	/**
	 * select all post.
	 */
	public void selectAllPanel()
	{
		setAllPostSelection(true);
	}
	
	/**
	 * unselect all post.
	 */
	public void clearSelectAllPanel()
	{
		setAllPostSelection(false);
	}
	
	/**
	 * enable bold all post. 
	 */
	public void enableAllBold()
	{
		setAllPostBold(true);
	}
	
	/**
	 * disable bold all post.
	 */
	public void disableAllBold()
	{
		setAllPostBold(false);
	}
	
	
	/**
	 * initialize.
	 */
	private void init()
	{
		addStyleName(DefaultStyleName);
		
		setWidget(_mainPanel);
	}
	
	/**
	 * create post panel.
	 * 
	 * @param post post
	 * @return post panel
	 */
	private Panel createPostPanel(Post post)
	{
		PostPanel panel = new PostPanel(post);
		_postPanels.add(panel);
		
		return panel;
	}
	
	/**
	 * select/unselect all post.
	 * 
	 * @param isSelected select is true
	 */
	private void setAllPostSelection(boolean isSelected)
	{
		for (PostPanel each : _postPanels)
		{
			each.changeSelectState(isSelected);
		}
	}
	
	/**
	 * enable/disable bold all post.
	 * 
	 * @param isEnable enable is true
	 */
	private void setAllPostBold(boolean isEnable)
	{
		for (PostPanel each : _postPanels)
		{
			each.setIsBold(isEnable);
		}
	}
}
